package com.css.fxfzypg.modules.rating.expertRating.dao;

import cn.hutool.core.date.DateUtil;
import com.css.fxfzypg.modules.rating.expertRating.model.entity.KpRatingTaskExpert;
import com.css.fxfzypg.modules.rating.expertRating.model.entity.KpRatingTaskExpertScoreProgress;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class KpRatingTaskExpertScoreProgressDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public Integer countSubmittedNum(List<String> currProvGlTaskIdList) {
        String sql = "SELECT count(*) from (select count(*) num from kp_rating_task_expert_score_progress t1 left join kp_rating_task_expert t2 on t1.krte_id = t2.id where (status = '2' or status = '3') and krtyt_id in ('" + StringUtils.join(currProvGlTaskIdList, "','") + "') GROUP BY t2.expert_id) t3 where t3.num = " + currProvGlTaskIdList.size();
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    public void updateById(KpRatingTaskExpertScoreProgress kpRatingTaskExpertScoreProgress) {
        StringBuilder sql = new StringBuilder("update kp_rating_task_expert_score_progress set ");
        if (ObjectUtils.isNotEmpty(kpRatingTaskExpertScoreProgress.getId())) {
            sql.append("  id = '").append(kpRatingTaskExpertScoreProgress.getId()).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskExpertScoreProgress.getKrteId())) {
            sql.append("  krte_id = '").append(kpRatingTaskExpertScoreProgress.getKrteId()).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskExpertScoreProgress.getKrtytId())) {
            sql.append("  krtyt_id = '").append(kpRatingTaskExpertScoreProgress.getKrtytId()).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskExpertScoreProgress.getStatus())) {
            sql.append("  status = '").append(kpRatingTaskExpertScoreProgress.getStatus()).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskExpertScoreProgress.getCommitTime())) {
            sql.append("  commit_time = '").append(DateUtil.format(kpRatingTaskExpertScoreProgress.getCommitTime(), "yyyy-MM-dd HH:mm:ss")).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskExpertScoreProgress.getIsCommit())) {
            sql.append("  is_commit = '").append(kpRatingTaskExpertScoreProgress.getIsCommit()).append("', ");
        }
        sql.deleteCharAt(sql.lastIndexOf(", "));
        sql.append(" where id = '").append(kpRatingTaskExpertScoreProgress.getId()).append("' ");
        jdbcTemplate.update(sql.toString());
    }

    public KpRatingTaskExpertScoreProgress getByExpertAndKrtytId(String krtytId, String expertId) {
        return jdbcTemplate.queryForObject("SELECT t1.* from kp_rating_task_expert_score_progress t1 LEFT JOIN kp_rating_task_expert t2 on t2.id = t1.krte_id where t1.krtyt_id = '" + krtytId + "' and t2.expert_id = '" + expertId + "'", new BeanPropertyRowMapper<>(KpRatingTaskExpertScoreProgress.class));
    }

    public void batchSave(List<KpRatingTaskExpertScoreProgress> list) {
        String sql = "INSERT INTO kp_rating_task_expert_score_progress(id, krte_id, krtyt_id, status, commit_time, is_commit) values(:id, :krteId, :krtytId, :status, :commitTime, :isCommit)";
        namedParameterJdbcTemplate.batchUpdate(sql, SqlParameterSourceUtils.createBatch(list.toArray()));
    }

    public void updateByKrtytId(KpRatingTaskExpertScoreProgress kpRatingTaskExpertScoreProgress) {
        StringBuilder sql = new StringBuilder("update kp_rating_task_expert_score_progress set ");
        if (ObjectUtils.isNotEmpty(kpRatingTaskExpertScoreProgress.getKrteId())) {
            sql.append("  krte_id = '").append(kpRatingTaskExpertScoreProgress.getKrteId()).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskExpertScoreProgress.getStatus())) {
            sql.append("  status = '").append(kpRatingTaskExpertScoreProgress.getStatus()).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskExpertScoreProgress.getCommitTime())) {
            sql.append("  commit_time = '").append(DateUtil.format(kpRatingTaskExpertScoreProgress.getCommitTime(), "yyyy-MM-dd HH:mm:ss")).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskExpertScoreProgress.getIsCommit())) {
            sql.append("  is_commit = '").append(kpRatingTaskExpertScoreProgress.getIsCommit()).append("', ");
        }
        sql.deleteCharAt(sql.lastIndexOf(", "));
        sql.append(" where krtyt_id = '").append(kpRatingTaskExpertScoreProgress.getKrtytId()).append("' ");
        jdbcTemplate.update(sql.toString());
    }

    public List<KpRatingTaskExpertScoreProgress> getCommitByKrtytId(String krtytId) {
        String sql = "SELECT * from kp_rating_task_expert_score_progress where is_commit = 1 and krtyt_id = ?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KpRatingTaskExpertScoreProgress.class), krtytId);
    }

    public Integer getExportComNumByKrtytId(String krtytId) {
        String sql = null;
        try {
            sql = "SELECT count(*) from kp_rating_task_expert_score_progress where is_commit = 1 and krtyt_id = ?";
            return jdbcTemplate.queryForObject(sql, Integer.class, krtytId);
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }
    }
}
